﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL.Models;
using DAL.Models.Enums;

namespace DAL.DBManagers
{
    public class SalesManager : BaseManager<Sale>
    {
        public SalesManager()
            : base()
        {
        }

        public override IEnumerable<Sale> GetAll()
        {
            return base.GetAll().Where(w => w.IsActive).ToList();
        }

        public List<SaleHelper> GetNavigationModeSales(Guid ServiceID)
        {
            PortEntities entity = _context as PortEntities;

            return (from s in entity.Sales
                    join n in entity.NavigationModes on s.ThingID equals n.NavigationModeID
                    where s.IsActive && n.IsActive && s.ServiceID == ServiceID &&
                          s.SaleTypeID == (int)SaleTypes.NAVIGATION_MODE
                    select new SaleHelper
                    {
                        ID = s.SaleID,
                        Name = n.Name,
                        //NameGeo = n.NameGeo,
                        Discount = s.Discount
                    }).ToList();
        }

        public List<SaleHelper> GetVesselCodeSales(Guid ServiceID)
        {
            PortEntities entity = _context as PortEntities;

            return (from s in entity.Sales
                    join v in entity.VesselCodes on s.ThingID equals v.VesselCodeID
                    where s.IsActive && v.IsActive && s.ServiceID == ServiceID &&
                          s.SaleTypeID == (int)SaleTypes.VESSELCODE
                    select new SaleHelper
                    {
                        ID = s.SaleID,
                        Name = v.Name,
                        //NameGeo = v.NameGeo,
                        Discount = s.Discount
                    }).ToList();
        }

        public List<SaleHelper> GetAdditionalForSales(Guid ServiceID)
        {
            PortEntities entity = _context as PortEntities;

            return (from s in entity.Sales
                    join a in entity.AdditionalParamForSales on s.ThingID equals a.AdditionalParamForSaleID
                    where s.IsActive && a.IsActive && s.ServiceID == ServiceID &&
                          s.SaleTypeID == (int)SaleTypes.ADDITIONALPARAMFORSALE
                    select new SaleHelper
                    {
                        ID = s.SaleID,
                        Name = a.Name,
                        //NameGeo = a.NameGeo,
                        Discount = s.Discount
                    }).ToList();
        }
        
        public void Edit(Guid ID, double Discount, Guid UserID)
        {
            var sale = base.Single(s => s.SaleID == ID);

            sale.Discount = Discount;
            sale.LastModifyDate = DateTime.Now;
            sale.LastModifier = UserID;

            base.SaveChanges();
        }

        public void Delete(Guid ID, Guid UserID)
        {
            var sale = base.Single(s => s.SaleID == ID);

            sale.IsActive = false;
            sale.LastModifyDate = DateTime.Now;
            sale.LastModifier = UserID;

            base.SaveChanges();
        }
    }
}
